<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" >
			
			<view class="wrapper" @click="choosetodefault(item)">
				<view class="u-box">
					<text class="name" style="color: #000000;">{{item.name}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>
				<view class="address-box">
					<text v-if="item.is_default == 1" class="tag">默认地址</text>
					<text class="address">{{item.city_name}} {{item.district_name}}{{item.address}}</text>
				</view>
			</view>
			
			<view v-if="selectaddress == '' || selectaddress == undefined">
				<view class="pull-right webkit">
					<view @tap="jump" :data-id="item.id">
						<image src="../../static/edit.png"></image> 
						<text>编辑</text>
					</view>
					<view @click="del(item.id)">
						<image src="../../static/delete.png"></image>
						<text>删除</text>
					</view>
				</view>
			</view>
			
			
		</view>
		
		<button class="add-btn" @click="jump">+新增收货地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list:[],
				user_id:'',
				res:[],
				// uid:{},
				source: 0,
				addressList: [],
				id:[],
				obj:{},
				selectaddress: '',
				goodsmsg: {}, // 商品页跳转过来的商品信息
				choosetodefault_item: 0, // 如果用户选择了变为1,1就不执行页面销毁函数 0执行
			}
		},
		onLoad(e){
			uni.getStorage({
				key: 'goodsmsg',
				success: (res) => {
					this.goodsmsg = res.data
				},
			});
			// 挑选地址
			if (e.selectaddress == 1) {
				this.selectaddress = e.selectaddress
			}
			uni.getStorage({
				key: 'user_id',
				success: (res) => {
				 this.user_id = res.data
				 this.setajax()
				},
			});
		},
		onShow() {
			this.setajax()
		},
		onPullDownRefresh() {
			this.setajax()
			setTimeout(function () {
					uni.stopPullDownRefresh();
			}, 1000);
		},
		// onUnload: function () { // 页面返回或者销毁 执行的函数
		// 	let o = this
		// 	if ((this.selectaddress == 1 && this.choosetodefault_item != 1) || this.address_back != 0) {
		// 		console.log(11)
		// 		uni.redirectTo({
		// 			url: '/pages/order/createOrder?orderid=' + o.goodsmsg.orderid + '&number=' + o.goodsmsg.number + '&sure=' + o.goodsmsg.sure + '&level_id=' + o.goodsmsg.level_id + '&group_id=' + o.goodsmsg.group_id + '&off2=2' + '&address_back=1'
		// 		})
		// 		return
		// 	}
		// 	return
		// },
		methods: {
			setajax(){
				var o = this
				uni.request({
					url: 'https://cx.mingmeijt.com/Api/Address/index?p=1',
					method: 'POST',
					data: {
						user_id: this.user_id,
						session_id: uni.getStorageSync("sessionid")
					},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						if(res.data.code == 999){
							uni.navigateTo({
								url:'/pages/999/999'
							})
						};
						if (res.data.data.list) {
							this.addressList = res.data.data.list
							let arr = [];
							this.addressList.forEach(function(item){
							 arr.push({id: item.id})
							})
							var obj = [];
							for(let i=0;i<arr.length;i++){
								o.obj = arr[i].id
							}
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			jump(e){
				if (this.selectaddress == 1) {
					uni.navigateTo({
					 	url: 'addressManage?id='+ e.currentTarget.dataset.id + '&selectaddress=1'
					});
				}
				 // event.stopPropagation();
				uni.navigateTo({
				 	url: 'addressManage?id='+ e.currentTarget.dataset.id
				});
			},
			del(id){
				var o = this
				uni.showModal({
					content: "是否删除地址",
					confirmText: "确定",
					cancelText: "取消",
					success: res => {
						if(res.confirm){
							uni.showLoading()
							uni.request({
								url: 'https://cx.mingmeijt.com/Api/Address/deleteAddress',
								method: 'POST',
								data: {
									'user_id': this.user_id,
									'id': id,
									'session_id': uni.getStorageSync("sessionid")
									},
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									uni.hideLoading()
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									if(res.data.code == 200) {
										setTimeout(function() {
											o.setajax()
										}, 1500);
									}
								},
								fail: () => {},
								complete: () => {}
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			choosetodefault(item) {
				 if ( this.selectaddress != 1 ) {
					 return
				 } else {
					// 更改地址为默认地址,然后再返回下单页
					let o = this
					uni.request({
						url: 'https://cx.mingmeijt.com/Api/Address/updateAddress',
						method: 'POST',
						data: {
							user_id: this.user_id,
							id: item.id,
							name: item.name,
							province_id: item.province_id,
							city_id: item.city_id,
							district_id: item.district_id,
							address: item.address,
							mobile: item.mobile,
							is_default: 1,
							session_id: uni.getStorageSync("sessionid")
						},
						header:{'Content-Type':'application/x-www-form-urlencoded'},
						success: res => {
							if(res.data.code == 999){
								uni.navigateTo({
									url:'/pages/999/999'
								})
							};
							uni.showToast({
								title: res.data.msg,
								icon: 'none'
							});
							if(res.data.code == 200) {
								setTimeout(function() {
									o.choosetodefault_item = 1
									uni.redirectTo({
										url: '/pages/order/createOrder?orderid=' + o.goodsmsg.orderid + '&number=' + o.goodsmsg.number + '&sure=' + o.goodsmsg.sure + '&level_id=' + o.goodsmsg.level_id + '&group_id=' + o.goodsmsg.group_id
									})
								}, 1200);
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: #000;
			margin-right: 10upx;
			background: #fff;
			border: 1px solid #000;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	.pull-right{float:right;}
	.pull-right view{margin-left:20upx;}
	.pull-right view image{width:34upx;height:34upx;position:relative;top:6upx;}
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 36upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: #000;
		border-radius: 10upx;
		/* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);		 */
	}
</style>
